#include <Ape/IO/FS/FSObject.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <Ape/IO/FS/EFileNotFound.h>
#include <Ape/ENotImplemented.h>
#include <Ape/IO/Console.h>

namespace Ape {
	namespace IO {
		namespace FS {
		
			FSObject::FSObject() {
			}
			FSObject::FSObject (const Path& path) : m_Path (path) {
			}
			FSObject::~FSObject() {
			}
			
			const struct stat& FSObject::get_Stat() const {
					char* buff = NULL;
					m_Path.ToString().ToLatin1 (&buff);
					int code = stat (buff, &m_Stat);
					if (code) {
						throw EFileNotFound (L"File not found: `" + m_Path.ToString() + L"'");
					}
					return m_Stat;
				}
				
			bool FSObject::get_Exists() const {
				try {
					get_Stat();
					return true;
				}
				catch (EFileNotFound&) {
					return false;
				}
			}
			
			FileType FSObject::get_FType() const {
				mode_t stMode = get_Stat().st_mode;
				if (stMode && S_IFREG) {
					return Regular;
				}
				else if (stMode && S_IFDIR) {
					return Directory;
				}
				
				throw Ape::ENotImplemented (L"Support for this type of file is not implemented");
			}
			
			void FSObject::_Dump (bool verbose) const {
				Object::_Dump (verbose);
				
				DUMP_NON_V_TYPE;
				
				using Ape::IO::StdOut;
				using Ape::IO::NewLine;
				
				StdOut << L"\t\tPath:\t" << m_Path.ToString() << NewLine;
			}
		}
	}
}
